<?php
declare (strict_types = 1);

namespace app\model\table\talk;

use app\model\BaseModel;
use app\model\MyException;
use app\model\table\user\User;
use app\model\tablenot\tool\RedisService;
use think\Model;

/**
 * @mixin \think\Model
 */
class Talk extends BaseModel
{
    protected $table = 'app_talk';

    // 是否全表缓存到redis
    public $tableToRedis = false;

    // 该表缓存到redis的策略
    public $tableToRedisKey = [];

    public $joinWithList=array(
        'UserInfo'=>array(
            'selfColumn'=>'user_id',
            'joinObj'=> User::class,
            'joinColumn'=>'id',
            'selectColumn'=>'id,name'
        ),
    );

    public $limitRedisKey = "talk_limit";

    // 检查限制
    public function checkLimit($userId)
    {
        $redisKey = $this->limitRedisKey."_".$userId;
        $limitTime = 10;
        $num = RedisService::instance()->get($redisKey);
        if(!empty($num)){
            throw new MyException("发送消息太快,请稍后重试");
        }else{
            RedisService::instance()->set($redisKey, 1, $limitTime);
        }
    }
}
